﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Blog.Core.IRepository;
using Blog.Core.IServices;
using Blog.Core.Model.Models;

namespace Blog.Core.Services
{
    public class RoleModulePermissionServices : Base.BaseServices<Model.Models.RoleModulePermission>, IRoleModulePermissionServices
    {
        IModulesRepository _modulesRepository;
        IRoleRepository _roleRepository;

        public RoleModulePermissionServices(IRoleModulePermissionRepository roleModulePermissionRepository, IModulesRepository modulesRepository, IRoleRepository roleRepository)
        {
            this.BaseDal = roleModulePermissionRepository;
            this._modulesRepository = modulesRepository;
            this._roleRepository = roleRepository;
        }

        /// <summary>
        /// 获取全部 角色接口(按钮)关系数据 注意我使用咱们之前的AOP缓存，很好的应用上了
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleModulePermission>> GetRoleModuleAsync()
        {
            var roleModulePermissions = await this.BaseDal.Query(a => a.IsDeleted == false);
            if (roleModulePermissions.Count > 0)
            {
                foreach (var item in roleModulePermissions)
                {
                    item.Role = await this._roleRepository.QueryByID(item.RoleId);
                    item.Module = await this._modulesRepository.QueryByID(item.ModuleId);
                }

            }
            return roleModulePermissions;
        }
    }
}
